﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MediaManager
{
    public partial class MDIManage : Form
    {
        
        public static Manage _manage = null;
        public MDIManage()
        {
            InitializeComponent();
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            if (null == _manage || _manage.IsDisposed)
            {
                _manage = new Manage(_userName);
            }
            else
            {
                _manage.btnAdd_Click(sender, e);
            }
            _manage.MdiParent = this;
            _manage.Show();
        }


        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
  
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStrip.Visible = toolBarToolStripMenuItem.Checked;
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.MdiParent = this;
            about.Show();
        }

        private void MDIManage_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "       登陆时间：" + DateTime.Now.ToString();
            Init();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (null == _manage || _manage.IsDisposed)
            {
                GlobalBase.Global.Instance.ShowMessage("请先添加个人信息！");
                return; 
            }
            _manage.btnSave_Click(sender, e);
        }
        SearchManage _searchManage = null;
        private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (null == _manage || _manage.IsDisposed)
            {
                _manage = new Manage(_userName);
                _manage.MdiParent = this;
            }
            _searchManage = new SearchManage(_manage);
            _searchManage.MdiParent = this;
            
            _searchManage.onUpdateData += new UpdateData(_manage.UpdateMediaData);
            _searchManage.Show();
        }

        private void toolStripChaKan_Click(object sender, EventArgs e)
        {
            if (null == _searchManage || _searchManage.IsDisposed)
            {
                GlobalBase.Global.Instance.ShowMessage("请先打开查询管理页面！");
                return;
            }
            _searchManage.btnUpdate_Click(sender, e);
        }

        private void toolStripShanChu_Click(object sender, EventArgs e)
        {
            if (null == _searchManage || _searchManage.IsDisposed)
            {
                GlobalBase.Global.Instance.ShowMessage("请先打开查询管理页面！");
                return;
            }
            _searchManage.btnDel_Click(sender, e);
        }

        private void toolStripMenuPrint_Click(object sender, EventArgs e)
        {
            try
            {
                if (null == _searchManage || _searchManage.IsDisposed)
                {
                    GlobalBase.Global.Instance.ShowMessage("请先打开查询管理页面！");
                    return;
                }
                _searchManage.Print(_userName);
            }
            catch (Exception ex)
            {
                GlobalBase.CatchException.DealWithException(ex);
                GlobalBase.Global.Instance.ShowMessage(ex.Message);
            }
        }

        private void toolStripMenuLogin_Click(object sender, EventArgs e)
        {
            LoginFrm loginFrm = new LoginFrm();
            loginFrm.MdiParent = this;
            loginFrm.onSendLoginSuc += new SendLoginSuc(GoInit);
            loginFrm.Show();

        }
        private void Init()
        {
            //Memnu
            fileMenu.Enabled = false;
            toolStripMenUserManager.Enabled = false;
            viewMenu.Enabled = false;
            windowsMenu.Enabled = false;
            toolStripMenuPrint.Enabled = false;
            toolStripMenuDay.Enabled = false;
            //toolbar
            newToolStripButton.Enabled = false;
            saveToolStripButton.Enabled = false;
            printPreviewToolStripButton.Enabled = false;
            toolStripButton1.Enabled = false;
            toolStripButton2.Enabled = false;
            toolPrint.Enabled = false;
        }

        private void GoInit(string userName)
        {
            //Memnu
            fileMenu.Enabled = true; ;
            toolStripMenUserManager.Enabled = true;
            viewMenu.Enabled = true;
            windowsMenu.Enabled = true;
            toolStripMenuPrint.Enabled = true;
            toolStripMenuDay.Enabled = true;
            //toolbar
            newToolStripButton.Enabled = true;
            saveToolStripButton.Enabled = true;
            printPreviewToolStripButton.Enabled = true;
            toolStripButton1.Enabled = true;
            toolStripButton2.Enabled = true;
            toolPrint.Enabled = true;
            _userName = userName;
            Global.Instance.UserName = userName;
            Global.Instance.MdiManage = this;
            string ss = "登陆用户：{0}" + toolStripStatusLabel1.Text;
            ss = string.Format(ss, userName);
            toolStripStatusLabel1.Text = ss;
        }
        private string _userName = "";
        private void 密码更改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UpdatePassword updatePassword = new UpdatePassword(_userName);
            updatePassword.MdiParent = this;
            updatePassword.Show();
        }

        private void 添加用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddUserFrm addUserFrm = new AddUserFrm(_userName);
            addUserFrm.MdiParent = this;
            addUserFrm.Show();
        }

        private void 删除用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DelFrm delFrm = new DelFrm(_userName);
            delFrm.MdiParent = this;
            delFrm.Show();
        }

        private void toolStripMenuDay_Click(object sender, EventArgs e)
        {
            DayInfoFrm dayInfoFrm = new DayInfoFrm(_userName);
            dayInfoFrm.MdiParent = this;
            dayInfoFrm.Focus();
            dayInfoFrm.Show();
        }

        private void MDIManage_FormClosed(object sender, FormClosedEventArgs e)
        {
            string s = string.Format(StructDayInfo.退出登陆, DateTime.Now);
            InserDayInfo(s);
        }
        private void InserDayInfo(string dInfo)
        {
            DataClasses2DataContext dataContext = new DataClasses2DataContext();
            DayInfo dayInfo = new DayInfo();
            dayInfo.id = dataContext.GetDayInfoIndex();
            dayInfo.name = _userName;
            dayInfo.操作 = dInfo;
            dayInfo.日期 = DateTime.Now;
            dataContext.DayInfo.InsertOnSubmit(dayInfo);
            dataContext.SubmitChanges();
        }
        private DongmaiSearchFrm _dongmaiSearchfrm = null;
        private void 动脉系统查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (null == _manage || _manage.IsDisposed)
            {
                _manage = new Manage(_userName);
                _manage.MdiParent = this;
            }
            if (null == _dongmaiSearchfrm || _dongmaiSearchfrm.IsDisposed)
            {
                _dongmaiSearchfrm = new DongmaiSearchFrm(_manage);
            }
            _dongmaiSearchfrm.MdiParent = this;
            _dongmaiSearchfrm.Focus();
            _dongmaiSearchfrm.Show();
        }
        private JingmaiSearchFrm _jingmaiSearchfrm = null;
        private void 静脉系统查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (null == _manage || _manage.IsDisposed)
            {
                _manage = new Manage(_userName);
                _manage.MdiParent = this;
            }
            if (null == _jingmaiSearchfrm || _jingmaiSearchfrm.IsDisposed)
            {
                _jingmaiSearchfrm = new JingmaiSearchFrm(_manage);
            }
            _jingmaiSearchfrm.MdiParent = this;
            _jingmaiSearchfrm.Focus();
            _jingmaiSearchfrm.Show();
        }

        private JubuSearchFrm _jubuSearchFrm = null;
        private void 局部合并症查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (null == _manage || _manage.IsDisposed)
            {
                _manage = new Manage(_userName);
                _manage.MdiParent = this;
            }
            if (null == _jubuSearchFrm || _jubuSearchFrm.IsDisposed)
            {
                _jubuSearchFrm = new JubuSearchFrm(_manage);
            }
            _jubuSearchFrm.MdiParent = this;
            _jubuSearchFrm.Focus();
            _jubuSearchFrm.Show();
        }

        private void MDIManage_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("确认要关闭吗？", "信息", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }
        }
        private ZhuankeSearchFrm _zhuankeSearchFrm = null;
        private void 专科与治疗方式查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (null == _zhuankeSearchFrm || _zhuankeSearchFrm.IsDisposed)
            {
                _zhuankeSearchFrm = new ZhuankeSearchFrm();
            }
            _zhuankeSearchFrm.MdiParent = this;
            _zhuankeSearchFrm.Focus();
            _zhuankeSearchFrm.Show();
        }
    }
}
